﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using TimeSheet.UI.Data;

namespace TimeSheet.UI
{
    /// <summary>
    /// Window form to manage all projects and categories
    /// </summary>
    public partial class ProjectsWindow : ChildWindow
    {
        private Data.Factory _factory = null;
        private const string _selectedError = "Please, select a project or a category.";

        /// <summary>
        /// Initialize a new instance of Project window form
        /// </summary>
        /// <param name="factory"></param>
        public ProjectsWindow(Factory factory)
        {
            _factory = factory;

            InitializeComponent();            

            this.DataContext = _factory;
            tvwProject.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(tvwProject_SelectedItemChanged);
            tvwProject_SelectedItemChanged(this, new RoutedPropertyChangedEventArgs<object>(null, null));            
        }

        /// <summary>
        /// Display the selected item in detail panel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvwProject_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            Category category = e.NewValue as Category;
            Project project = e.NewValue as Project;

            // Default button states
            btnAddCategory.IsEnabled = true;
            btnAddProject.IsEnabled = true;
            btnDelete.IsEnabled = true;
            btnMoveUp.IsEnabled = true;
            btnMoveDown.IsEnabled = true;

            // Category selected
            if (category != null)
            {
                panDetail.DataContext = category;
                lblReference.IsEnabled = false;
                txtReference.IsEnabled = false;

                // Buttons Up/Down
                if (_factory.Categories.IndexOf(category) <= 0) btnMoveUp.IsEnabled = false;
                if (_factory.Categories.IndexOf(category) >= _factory.Categories.Count - 1) btnMoveDown.IsEnabled = false;
            }

            // Project selected
            else if (project != null)
            {
                panDetail.DataContext = project;
                lblReference.IsEnabled = true;
                txtReference.IsEnabled = true;

                // Buttons Up/Down
                category = _factory.Categories.Where(c => c.Projects.Contains(project)).First();
                if (category.Projects.IndexOf(project) <= 0) btnMoveUp.IsEnabled = false;
                if (category.Projects.IndexOf(project) >= category.Projects.Count - 1) btnMoveDown.IsEnabled = false;
            }

            else
            {
                btnAddProject.IsEnabled = false;
                btnDelete.IsEnabled = false;
                btnMoveUp.IsEnabled = false;
                btnMoveDown.IsEnabled = false;                
            }

        }

        /// <summary>
        /// Close the window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        /// <summary>
        /// Close the window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        /// Delete project or category button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            Category category = tvwProject.SelectedValue as Category;
            Project project = tvwProject.SelectedValue as Project;

            // Remove this category and all associated projects
            if (category != null)
            {
                if (MessageBox.Show("Do you want remove this category and all projects associated ?", "Confirmation", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    _factory.Categories.Remove(category);
                }
            }

            // Removove this project
            else if (project != null)
            {
                if (MessageBox.Show("Do you want remove this project ?", "Confirmation", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    category = _factory.Categories.Where(c => c.Projects.Contains(project)).First();
                    category.Projects.Remove(project);
                }
            }

            // Error
            else
            {
                MessageBox.Show(_selectedError, "Error", MessageBoxButton.OK);
            }
        }

        /// <summary>
        /// Add a new empty category
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddCategory_Click(object sender, RoutedEventArgs e)
        {
            _factory.Categories.Add(new Data.Category() { Name = "New Category" });            
        }

        /// <summary>
        /// Add a new empty project
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddProject_Click(object sender, RoutedEventArgs e)
        {
            Category category = tvwProject.SelectedValue as Category;
            Project project = tvwProject.SelectedValue as Project;            
            
            Project newProject = new Project() { Name = "New Project" };

            // Add a new project for this category
            if (category != null)
            {                
                category.Projects.Add(newProject);
            }

            // Add a new project next this project
            else if (project != null)
            { 
                category = _factory.Categories.Where(c => c.Projects.Contains(project)).First();
                
                int index = category.Projects.IndexOf(project);
                category.Projects.Insert(index + 1, newProject);
            }

            // Error
            else
            {
                MessageBox.Show(_selectedError, "Error", MessageBoxButton.OK);
            }
            
        }

        /// <summary>
        /// Move the selected item (project or category) to up
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMoveUp_Click(object sender, RoutedEventArgs e)
        {
            Category category = tvwProject.SelectedValue as Category;
            Project project = tvwProject.SelectedValue as Project;

            // Move Up this category
            if (category != null)
            {
                int index = _factory.Categories.IndexOf(category);
                if (index > 0)
                {
                    _factory.Categories.RemoveAt(index);
                    _factory.Categories.Insert(index - 1, category);
                }
            }

            // Move Up this project
            else if (project != null)
            {
                category = _factory.Categories.Where(c => c.Projects.Contains(project)).First();

                int index = category.Projects.IndexOf(project);
                if (index > 0)
                {
                    category.Projects.RemoveAt(index);
                    category.Projects.Insert(index - 1, project);
                }
            }

            // Error
            else
            {
                MessageBox.Show(_selectedError, "Error", MessageBoxButton.OK);
            }
        }

        /// <summary>
        /// Move the selected item (project or category) to bottom
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMoveDown_Click(object sender, RoutedEventArgs e)
        {
            Category category = tvwProject.SelectedValue as Category;
            Project project = tvwProject.SelectedValue as Project;

            // Move Down this category
            if (category != null)
            {
                int index = _factory.Categories.IndexOf(category);
                if (index < _factory.Categories.Count - 1)
                {
                    _factory.Categories.RemoveAt(index);
                    _factory.Categories.Insert(index + 1, category);
                }
            }

            // Move Down this project
            else if (project != null)
            {
                category = _factory.Categories.Where(c => c.Projects.Contains(project)).First();

                int index = category.Projects.IndexOf(project);
                if (index < category.Projects.Count - 1)
                {
                    category.Projects.RemoveAt(index);
                    category.Projects.Insert(index + 1, project);
                }
            }

            // Error
            else
            {
                MessageBox.Show(_selectedError, "Error", MessageBoxButton.OK);
            }
        }
        
    }
}

